package com.lry.filter;

import com.lry.ExpressionType;
import org.apache.commons.lang3.StringUtils;
import com.lry.filter.expression.Expression;
import com.lry.filter.expression.MessageEvaluationContext;

import java.util.Map;

public class TagMessageFilter implements MessageFilter{

    @Override
    public boolean filter(Object... obj) {
        String expr = (String) obj[0];//消费者指定的表达式
        String tags = (String) obj[1];//消息里的tag
        Map<String,String> properties = (Map<String, String>) obj[2];

        if(StringUtils.isNotEmpty(expr)){
            if(expr.startsWith(ExpressionType.SQL92)){
                Expression compile = null;
                try {
                    compile = FilterFactory.INSTANCE.get(ExpressionType.SQL92).compile(expr.replace(ExpressionType.SQL92,""));
                    MessageEvaluationContext context = new MessageEvaluationContext(properties);
                    Object fb = compile.evaluate(context);
                    if(null!=fb&&(fb instanceof Boolean)){
                        boolean f = (boolean) fb;
                        //没有匹配上
                        if(!f){
                            return true;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }else{
                //处理tag 消息
                if(!expr.equalsIgnoreCase(tags)){
                    return true;
                }
            }
        }
        return false;
    }

}
